package cn.yy.cloudpay.view;


import cn.yy.cloudpay.R;
import cn.yy.cloudpay.listener.ICustomDialogListener;
import cn.yy.cloudpay.util.Constant;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.AdapterView.OnItemClickListener;

public class PayTypeDialog extends Dialog {

	private static Integer payType = Constant.PAY_TYPE_ALIPAY;
	
	public PayTypeDialog(Context context) {
		super(context);
	}

	public PayTypeDialog(Context context, int theme) {
		super(context, theme);
	}

	public static class Builder {
		private Context context;
		private LayoutInflater inflater;
		private MyGridView gridview;
		private ICustomDialogListener listener;

		public Builder(Context context) {
			payType = Constant.PAY_TYPE_ALIPAY;
			this.context = context;
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public PayTypeDialog create() {
			// instantiate the dialog with the custom Theme
			final PayTypeDialog dialog = new PayTypeDialog(context,R.style.Dialog);
			View layout = inflater.inflate(R.layout.layout_paytype_dialog, null);
			String[] img_text = { "支付宝", "微信", "刷卡消费"};
			int[] imgs = { R.drawable.pt_alipay, R.drawable.pt_micropay,R.drawable.pt_card};
			gridview=(MyGridView) layout.findViewById(R.id.gridview);
			gridview.setAdapter(new MyGridAdapter(context, img_text, imgs));
			gridview.setOnItemClickListener(new OnItemClickListener(){
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
						long arg3) {
					// TODO Auto-generated method stub
					switch(arg2){
					case 0:
						payType = Constant.PAY_TYPE_ALIPAY;
						break;
					case 1:
						payType = Constant.PAY_TYPE_MICROPAY;
						break;
					case 2:
						payType = Constant.PAY_TYPE_BANKCARD;
						break;
					}
					listener.onPositiveClickListener(dialog,arg1,payType);
				}
			});
			
			Button btnCancel = (Button) layout.findViewById(R.id.btn_layout_paytypedialog_negative);
			btnCancel.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					listener.onNegativeClickListener(dialog);
				}
			});
			
			dialog.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			//点击屏幕对话框不会取消
			dialog.setCanceledOnTouchOutside(false);
			return dialog;
		}
		
		public void setOnDialogClickListener(ICustomDialogListener listener) {
			this.listener = listener;
		}
	}
}